const app = getApp()
import {
  workList,
  workFinish,
  userInfo
} from '../../config/https'
//工地列表、师傅确认完工
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isMaster: false,   //是否为师傅
    list: [],
    page: 0,
    pagesize: 100
  },
  getUserInfo(){
    userInfo({
      token: wx.getStorageSync('token')
    }).then(res => {
      if(res.code == 1){
        this.setData({
          isMaster: res.data.welcome.user_type == 1 ? true : false
        })
      }
    })
  },
  getData() {
    wx.showLoading({
      title: '加载中',
    })
    workList({
      token: wx.getStorageSync('token'),
      page: ++this.data.page,
      pagesize: this.data.pagesize
    }).then(res => {
      wx.hideLoading()
      if (res.code == 1) {
        this.setData({
          list: [...this.data.list, ...res.data]
        })
      }
    })
  },
  //预览当前图片
  previewImg(e) {
    let index = e.currentTarget.dataset.index
    let imgUrl = e.currentTarget.dataset.imgurl
    let imgArr = this.data.list.detail[index].images
    wx.previewImage({
      current: imgUrl, //当前图片地址
      urls: imgArr, //所有要预览的图片地址
      success: function (res) {},
      fail: function (res) {}
    })
  },
  goupImg(e) {
    wx.navigateTo({
      url: '/pages/workerImg/workerImg?orderid=' + e.currentTarget.dataset.orderid,
    })
  },
  //获取客户电话
  callPhone(e) {
    let phone = e.currentTarget.dataset.phone
    wx.showModal({
      title: '客户电话',
      content: '是否拨打客户电话？',
      confirmColor: '#2076F9',
      success(res) {
        if (res.confirm) {
          wx.makePhoneCall({
            phoneNumber: phone
          })
        }
      }
    })
  },
  //师傅确认完工
  finishWork(e) {
    let orderid = e.currentTarget.dataset.orderid
    wx.showModal({
      title: '温馨提示',
      content: '是否确认已完工，该操作不可撤回？',
      confirmColor: '#2076F9',
      success(res) {
        if (res.confirm) {
          workFinish({
            token: wx.getStorageSync('token'),
            order_id: orderid
          }).then(res => {
            app.$tip(res.msg)
            if (res.code == 1) {
              this.getOrderRob()
            }
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (!wx.getStorageSync('token')) {
      wx.showModal({
        title: '温馨提示',
        content: '你还未登入，请先登入',
        success (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/logs/logs',
            })
          }
        }
      })
    }else{
      this.setData({
        page: 0,
        list: []
      })
      this.getUserInfo()
      this.getData()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getData()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})